<?php
function listItems(){
    $query = "SELECT * FROM `blocks` ";
    $PDO = DB_LINK::getInstance();
    $stm = $PDO->prepare($query);
    $stm->execute();
    $raw = $stm->fetchAll();
    $stm->closeCursor();
    $contentMapper = new blockContent_Mapper();
    $collection = new blockContent_Mapper_Collection($raw, $contentMapper);
    $tpl = XTemplate::getInstance();
    $tpl->assign('admin_listing_title', 'Editeaza blocurile');
    $template = "general/admin_listing.html";
    $tpl->assign_file('CONTENT',$template);
    $tpl->assign('adauga','<a href="/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=edit&objectId=0">Adauga bloc</a>');
    
    $index=0;
    foreach ($collection as $object) {
        $index++;
        $actions = array(
            '<a href="/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=edit&objectId='.$object->getId().'">editeaza</a>',
            '<a href="/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=delete&objectId='.$object->getId().'">sterge</a>'
        );
        
        $color = $index % 2 == 0 ? 'eee' : 'ddd';
        $tpl->assign('color',$color);
        $tpl->assign('denumire', $object->getName());
        $tpl->assign('editeaza', '<a href="/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=edit&objectId='.$object->getId().'">editeaza</a>');
        $tpl->assign('sterge', '<a href="/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=delete&objectId='.$object->getId().'">sterge</a>');
        $tpl->parse('main.admin_listing.tr');
    }    
    $tpl->parse('main.admin_listing');
   
    
}

function editItem(){
    require_once(REAL_PATH . "/classes/PFBC/Form.php");
    $tpl = Xtemplate::getInstance();
    $form = new Form("Version2x",500);
    $requestUri = SERVER_PATH.$_SERVER['REQUEST_URI'];    
    if($requestUri != @$_SERVER['HTTP_REFERER']){
        $form->clearValues("Version2x");
        $form->clearErrors("Version2x");
    }

    $form->configure(array(
        "method" => "post",        
        "action" => ""
    ));    
    $contentMapper = new blockContent_Mapper();
    if($_GET['objectId'] > 0){
        $obj = $contentMapper->findById($_GET['objectId']);
        
    } else {
        $obj = new blockContentObject();
    }

    $form->addElement(new Element_Hidden("style", array(
        'value' => '',
    )));

    $form->addElement(new Element_Textbox("Nume:", "name", array(
        'value' => $obj->getName(),
        'validation' => array(new Validation_Required)
    )));

    if($obj->getName() != 'index'){
        $form->addElement(new Element_Textbox("Titlu:", "title", array(
            'value' => $obj->getTitle()            
        )));
    } 

    
    $tinyMCE = new Element_TinyMCE("Body:", "body", array(
        'value' => $obj->getBody(),
        'validation' => array(new Validation_Required)
    ));
    $tinyMCE->setWidth('600px');
    $form->addElement($tinyMCE);

    /*$form->addElement(new Element_Textbox("Style:", "style", array(
        'value' => $obj->getStyle()       
    )));*/

    //verific caractrere speciale atat in form cat si in sql
    //verific ce se intampla cu datele din sesiune si cum se scot pentru formular valid
    $form->addElement(new Element_Button);    
    if(isset ($_POST) && count($_POST) > 1){
        if(Form::isValid("Version2x")) {
            $obj->setTitle($_POST['title']);
            //$obj->setTemplate($_POST['template']);
            $obj->setName($_POST['name']);
            $obj->setBody($_POST['body']);
            $obj->setStyle($_POST['style']);
            $obj = $contentMapper->save($obj);
            redirectToUrl(SERVER_PATH."/admin/?adminaction=edit_module&modulename=blocks&admin_module_action=edit&objectId={$obj->getId()}&ok=1");
        
        } else {

        }
    }
    $tpl->assign_file('CONTENT','modules/static_content/admin/form.html');
    $tpl->assign('title','Editeaza');
    if(isset($_GET['ok']) && $_GET['ok'] == 1){
        $tpl->assign('message_ok', 'Datele au fost salvate!');
        $tpl->parse("main.ok");
    }
    $tpl->assign('form', $form->render(true));
}

function admin_exec(){
    $adminModuleAction = "";
    if(isset ($_GET['admin_module_action']) && $_GET['admin_module_action'] != ""){
        $adminModuleAction = $_GET['admin_module_action'] ;
    }
    switch ($adminModuleAction){
        default: listItems();break;
        case 'edit': editItem();break;

    }
    
    
}
?>
